package cn.edu.swu.shop;

import cn.edu.swu.listener.SessionCounterListener;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(urlPatterns = {"/user/getOnlineUsers"})
public class OnlineUsersServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("application/json;charset=UTF-8");
        PrintWriter out = response.getWriter();

        int activeUsers = SessionCounterListener.getActiveSessions();
        out.write("{\"activeUsers\": " + activeUsers + "}");
    }
}
